import {
	requestNoAuth,
	request
} from '../../lib/java110/java110Request.js';
import url from '../../constant/url.js';

import mapping from '../../lib/java110/java110Mapping.js'



export function getUserAddress(_data) {
	return new Promise((resolve, reject) => {
		let moreRooms = [];
		request({
			url: url.queryUserAddress,
			method: "GET",
			data: _data, //动态数据
			success: function(res) {
				let _data = res.data;
				if (_data.code == 0) {
					resolve(_data);
					return;
				}
				reject(_data.msg);
			},
			fail: function(e) {
				reject(e);
			}
		});
	})
}

export function getShopVipUser(_data) {
	return new Promise((resolve, reject) => {
		let moreRooms = [];
		request({
			url: url.queryShopVipUser,
			method: "GET",
			data: _data, //动态数据
			success: function(res) {
				let _data = res.data;
				if (_data.code == 0) {
					resolve(_data);
					return;
				}
				reject(_data.msg);
			},
			fail: function(e) {
				reject(e);
			}
		});
	})
}

export function queryUserIntegral(_data) {
	return new Promise((resolve, reject) => {
		let moreRooms = [];
		request({
			url: url.queryUserIntegral,
			method: "GET",
			data: _data, //动态数据
			success: function(res) {
				let _data = res.data;
				if (_data.code == 0) {
					resolve(_data.data);
					return;
				}
				reject(_data.msg);
			},
			fail: function(e) {
				reject(e);
			}
		});
	})
}


/**
 * add by wuxw
 * @param {Object} _data 保存 用户地址
 */
export function saveUserVisit(_data) {
	return new Promise((resolve, reject) => {
		requestNoAuth({
			url: url.saveUserVisitRecord,
			method: "POST",
			data: _data, //动态数据
			success: function(res) {
				let _data = res.data;
				return;
			},
			fail: function(e) {
				reject(e);
			}
		});
	})
}

/**
 * add by wuxw
 * @param {Object} _data 保存 用户地址
 */
export function outUserLogin(_data) {
	return new Promise((resolve, reject) => {
		request({
			url: url.userLogout,
			method: "POST",
			data: _data, //动态数据
			success: function(res) {
				let _data = res;
				resolve(_data);
				return;
			},
			fail: function(e) {
				reject(e);
			}
		});
	})
}
/**
 * add by wuxw
 * @param {Object} _data 保存 用户地址
 */
export function saveUpdateUserAddress(_data) {

	return new Promise((resolve, reject) => {

		if (_data.username == ''){
			reject("联系人不能为空");
			return;
		} else if (_data.userId == '') {
			reject("用户不能为空");
			return;
		} else if (_data.areaCode == '') {
			reject("地区不能为空");
			return;
		} else if (_data.tel == '') {
			reject("手机号不能为空");
			return;
		} else if (_data.address == '') {
			reject("地址不能为空");
			return;
		} else if (_data.isDefault == '') {
			reject("默认地址不能为空");
			return;
		} else if (_data.postalCode == '') {
			reject("邮编不能为空");
			return;
		}
		let moreRooms = [];
		request({
			url: url.saveUserAddress,
			method: "POST",
			data: _data, //动态数据
			success: function(res) {
				let _data = res.data;
				if (_data.code == 0) {
					resolve(_data);
					return;
				}
				reject(_data.msg);
			},
			fail: function(e) {
				reject(e);
			}
		});
	})
}

/**
 * add by wuxw
 * @param {Object} _data 保存 用户地址
 */
export function deleteUserAddress(_data) {

	return new Promise((resolve, reject) => {

		if (_data.userId == '') {
			reject("用户不能为空");
			return;
		} else if (_data.addressId == '') {
			reject("地址不能为空");
			return;
		}
		request({
			url: url.deleteUserAddress,
			method: "POST",
			data: _data, //动态数据
			success: function(res) {
				let _data = res.data;
				if (_data.code == 0) {
					resolve(_data);
					return;
				}
				reject(_data.msg);
			},
			fail: function(e) {
				reject(e);
			}
		});
	})
}
/**
 * address: "青海省西宁市城中区申宁路"
	age: 30
	email: "9"
	name: ""
	sex: ""
	tel: ""
	token: ""
	total: 0
	userId: ""
	userName: "吴学文"
 * 获取用户信息
 */
export function getUserInfo() {
	return uni.getStorageSync(mapping.USER_INFO);
}
/**
 * 用户ID
 */
export function getUserId() {
	return getUserInfo().userId;
}
/**
 * 用户名称
 */
export function getUserName() {
	return getUserInfo().name;
}
/**
 * 用户手机号
 */
export function getUserTel() {
	return getUserInfo().tel;
}


/**
 * 注册用户
 * add by wuxw
 * @param {Object} _data 保存 用户地址
 */
export function registerUser(_data) {

	return new Promise((resolve, reject) => {
		requestNoAuth({
			url: url.register,
			method: "POST",
			data: _data, //动态数据
			success: function(res) {
				let _data = res.data;
				if (_data.code == 0) {
					resolve(_data);
					return;
				}
				reject(_data.msg);
			},
			fail: function(e) {
				reject(e);
			}
		});
	})
}

/**
 * 注册用户
 * add by wuxw
 * @param {Object} _data 保存 用户地址
 */
export function loginUser(_data) {

	return new Promise((resolve, reject) => {
		requestNoAuth({
			url: url.login,
			method: "POST",
			data: _data, //动态数据
			success: function(res) {
				let _data = res.data;
				if (_data.code == 0) {
					resolve(_data);
					return;
				}
				reject(_data.msg);
			},
			fail: function(e) {
				reject(e);
			}
		});
	})
}

export function wechatLogin(_data) {

	return new Promise((resolve, reject) => {
		requestNoAuth({
			url: url.phoneWechatLogin,
			method: "POST",
			data: _data, //动态数据
			success: function(res) {
				let _data = res.data;
				if (_data.code == 0) {
					resolve(_data);
					return;
				}
				reject(_data.msg);
			},
			fail: function(e) {
				reject(e);
			}
		});
	})
}





export function timer(_that) {
	let second = 60;
	let promise = new Promise((resolve, reject) => {
		let setTimer = setInterval(
			() => {
				second = second - 1;
				_that.btnValue = second + '秒';
				_that.btnDisabled = true;
				if (second <= 0) {
					second = 60;
					_that.btnValue = '获取验证码';
					_that.btnDisabled = false;
					resolve(setTimer)
				}
			}, 1000)
	})
	promise.then((setTimer) => {
		clearInterval(setTimer)
	})
}

export function sendSmsCode(_link, _that) {
	let obj = {
		tel: _link
	};
	if (obj.tel == '') {
		wx.showToast({
			title: '请输入手机号',
			icon: 'none',
			duration: 2000
		});
		return;
	}
	uni.showLoading({
		title: '加载中',
		mask: true
	});
	return new Promise(
		(resolve, reject) => {
			requestNoAuth({
				url: url.userSendSms,
				method: "POST",
				data: obj,
				//动态数据
				success: function(res) {
					uni.hideLoading();
					let _json = res.data;
					if (_json.code == 0) {
						wx.showToast({
							title: _json.data,
							icon: 'none',
							duration: 5000
						});
						wx.hideLoading();
						timer(_that);
						resolve();
						return;
					}
					wx.showToast({
						title: _json.msg,
						icon: 'none',
						duration: 5000
					});
					reject();
				},
				fail: function(e) {
					uni.hideLoading();
					reject();
				}
			});
		})
}


/**
 * 
 * @param {Object} _data
 */
export function queryShopUserAccountAndVip(_data) {
	return new Promise((resolve, reject) => {
		let moreRooms = [];
		request({
			url: url.queryShopUserAccountAndVip,
			method: "GET",
			data: _data, //动态数据
			success: function(res) {
				let _data = res.data;
				if (_data.code == 0) {
					resolve(_data);
					return;
				}
				reject(_data.msg);
			},
			fail: function(e) {
				reject(e);
			}
		});
	})
}

export function getShareWechatParam(_data) {
	return new Promise((resolve, reject) => {
		let moreRooms = [];
		requestNoAuth({
			url: url.getShareWechatParam,
			method: "GET",
			data: _data, //动态数据
			success: function(res) {
				let _data = res.data;
				if (_data.code == 0) {
					resolve(_data);
					return;
				}
				reject(_data.msg);
			},
			fail: function(e) {
				reject(e);
			}
		});
	})
}

export function queryUserPropertyFee(_data) {
	return new Promise((resolve, reject) => {
		let moreRooms = [];
		request({
			url: url.queryUserPropertyFee,
			method: "GET",
			data: _data, //动态数据
			success: function(res) {
				let _data = res.data;
				if (_data.code == 0) {
					resolve(_data);
					return;
				}
				reject(_data.msg);
			},
			fail: function(e) {
				reject(e);
			}
		});
	})
}


export function changeUserPwd(_data) {
	return new Promise((resolve, reject) => {
		request({
			url: url.changeUserPwd,
			method: "POST",
			data: _data, //动态数据
			success: function(res) {
				let _data = res.data;
				resolve(_data);
				return;
			},
			fail: function(e) {
				reject(e);
			}
		});
	})
}


